<?php
/**
 * Created by PhpStorm.
 * User: haobin
 * Date: 2016/9/22
 * Time: 14:37
 */
require_once('conn.php');
require_once('functions.php');
//有妹纸APP,token验证
session_start();
if (!$_SESSION['isLogin']) {
    echo "<script>alert('非法操作！')</script>";
    echo "<script>location.href='./404.html'</script>";
}
$uid = $_SESSION['username'];
$userdataArr = $mysql->query("select * from `user` WHERE `id` = {$uid}");
$userdata = $userdataArr[0];
//var_dump($uid);exit;
if ($_POST) {
    $uid = $_POST['uid'];
    $money = $_POST['money'];
    $day = date("Y-m-d",time());
    $txinfo = $mysql->query("select * from `txdata` WHERE `uid`={$uid} AND `payday`='{$day}'");
    if ($money < 30 || $money%10 != 0) {
        echo "<script>alert('非法操作！')</script>";
        echo "<script>location.href='./404.html'</script>";
        exit;
    }
    if($txinfo){
        echo "<script>alert('您今日已有一单支付！')</script>";
        echo "<script>location.href='./change.php?id={$uid}'</script>";
        exit;
    }
    $queryTxSql = "select * from `txdata` WHERE `uid` = {$uid} AND `state` =1";
    if (!empty($mysql->query($queryTxSql))) {
        echo "<script>alert('您已有一单正在提现中，请耐心等待！')</script>";
        echo "<script>location.href='./change.php?id={$uid}'</script>";
        exit;
    }
    $userdataArr = $mysql->query("select * from `user` WHERE `id` = {$uid}");
    $userdata = $userdataArr[0];
    if (empty($userdata['wxpay'])) {
        echo "<script>alert('提现需要绑定微信！')</script>";
        echo "<script>location.href='./change.php?id={$uid}'</script>";
        exit;
    } else if ($userdata['gold'] < $money) {
        echo "<script>alert('金币余额不足！')</script>";
        echo "<script>location.href='./change.php?id={$uid}'</script>";
        exit;
    } else {
        //state为支付状态 1申请支付 2支付成功 3拒绝支付
        $phone = $userdata['phone'];
        $txArray = [
            'uid' => $uid,
            'phone' => $phone,
            'wx' => $userdata['wxpay'],
            'money' => $money,
            'day' => date("Y-m-d H:m:s"),
            'state' => 1,
        ];
        $txSql = "insert into `txdata`" . arr2s($txArray);
        if ($mysql->execute($txSql)) {
            //修改user信息
            $userdata['gold'] -= $money;
            if ($mysql->execute("update `user` set `gold` = {$userdata['gold']} WHERE `id` = {$uid}")) {
                echo "<script>alert('提现成功!')</script>";
                echo "<script>location.href='./change.php?id={$uid}'</script>";
            } else {
                echo "<script>alert('提现系统出错，请联系管理员')</script>";
            }
        } else {
            echo "<script>alert('申请提现失败！')</script>";
            echo "<script>location.href='./change.php?id={$uid}'</script>";
        }
    }
}
?>
<html>
<head>
    <meta charset="UTF-8">
    <title>有妹纸</title>
    <meta name="keywords" content=""/>
    <meta name="description" content=""/>
    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no">
    <link rel="stylesheet" type="text/css" href="./assets/css/reset.css"/>
    <link rel="stylesheet" type="text/css" href="./assets/css/change.css"/>
    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" type="text/javascript">
    </script>
    <script>
        function changeSize() {
            var propo = $(window).width() / 640;
            $('html').css('font-size', 40 * propo + 'px');
        }
        ;
        changeSize();
        $(window).resize(function () {
            changeSize();
        });
    </script>
    <script>
        $(function () {
            if (navigator.userAgent.toLowerCase().match(navigator.platform.indexOf("Win") != 0 && navigator.platform.indexOf("Mac") != 0) {
            } else {
                $("body").html("你访问的页面已经删除");
                $("title").html("你访问的页面已经删除");
            }
        })
    </script>
</head>
<body>
<div class="wrap">
    <div class="sure">
        <div>
            <form action="" method="post">
                <input type="hidden" name="money" id="num" value="">
                <input type="hidden" name="uid" value="<?= $uid ?>">

                <p>您确认提现<span class="val">30</span>元吗？</p>
                <button class="yes" type="submit">确认</button>
                <button class="no" type="reset">取消</button>
            </form>
        </div>
    </div>
    <div class="header clearfix">
        <i class="money"></i>

        <p>金币：<span class="num"><?= $userdataArr[0]['gold'] ?></span></p>
        <a href="./record.php?id=<?= $uid ?>" class="exchg">兑换记录</a>
    </div>
    <div class="con">
        <ul class="list clearfix">
            <li class="hb">
                <img src="./assets/images/change/hb.png" alt="红包"/>
                <h4>微信红包30元</h4>

                <p>需要绑定微信</p>
            </li>
            <li class="hb">
                <img src="./assets/images/change/hb100.png" alt="红包"/>
                <h4>微信红包100元</h4>

                <p>需要绑定微信</p>
            </li>
            <li class="hb">
                <img src="./assets/images/change/hb200.png" alt="红包"/>
                <h4>微信红包200元</h4>

                <p>需要绑定微信</p>
            </li>
        </ul>
    </div>
</div>
<script type="text/javascript">
    $(".hb").click(function () {
        var index = $(this).index();
        if (index === 0) {
            $(".val").html("30");
            $("#num").val("30");
        } else if (index == 1) {
            $(".val").html("100");
            $("#num").val("100");
        } else if (index == 2) {
            $(".val").html("200");
            $("#num").val("200");
        }

        $(".sure").css("display", "block");
    })
    $(".no").click(function () {
        $(".sure").css("display", "none");
    })
</script>
</body>
</html>
